<template>
  <base-breadcrumb class="" :title="titleName">
    <!-- 资料管理列表 -->
    <a-spin :spinning="loading">
      <a-card :bordered="false">
          <title-name title="基本信息"></title-name>
          <custom-form
            ref="customForm"
            :isEmit="false"
            :form="form"
            :edit-model="'E'"
            :form-list="formList"
            :rules="rules"
            is-validate
            @selectOrg="selectOrg"
          />
      </a-card>
    </a-spin>
    <!-- 页脚操作栏 -->
    <div class="btn_box page-btn-right-top">
      <a-button :disabled="loading" :loading="defloading" v-if="editable" type="primary" class="button_right" @click="save"
        >保存草稿</a-button>

      <a-button :disabled="loading" :loading="subloading" v-if="editable" type="primary" class="button_right" @click="handleSubmit(1)"
        >提交</a-button
      >
      <a-button @click="$close">关闭</a-button>
    </div>
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
    </base-breadcrumb>
</template>

<script>
import UploadList from '@/components/UploadList'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import { addList, editList, getDetails, queryTechnicalDataManagementList } from '@/api/technical/dataManagePlan'

export default {
    name:'a'+Date.now(),
    components: {
      SelectSubCompany,
      SelectProject,
      UploadList,
    },
    data() {
        return {
            visible: {
              programForm: false
            },
            loading: false, // 加载控件
            defloading: false,
            subloading: false,
            form: {
              updateUserId: '',
              curId: "",
              technicalDataManagementFile: [],
            }, // 表单
            technicalDataManagementFileInfo: [],
            rules: {
                technicalDataManagementFile: [{ required: true, type: 'array', message: '请上传附件', trigger: 'change' }],
            }, // 校验规则
            //获取流程配置值
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            businessId: null
        }
    },

   async mounted() {
      if (this.type == 'add') {
        this.form.updateUserId = this.userInfo.id
        this.form.updateUserName = this.userInfo.name
        this.form.updateTime = this.$dateFormat(new Date(), 'D')
        this.form.curId = this.$userInfo().currentPost.id
      } else {
          this.getDetail()
      }
     //获取通过获取流程配置值
     this.processConfig = await this.$getConfigFn('technology_date_manager')
    },
    computed: {
      titleName() {
        const titlemap = {
          add: '新增',
          edit: '编辑',
        }
        return titlemap[this.type] || '查看'
      },
      type() {
        return this.$route.query.type
      },
      editable() {
        return this.type == 'add' || this.type == 'edit'
      },
      userInfo() {
        return this.$userInfo()
      },
      formList() {
        return [
          {
            label: '所属单位',
            type: 'orgTree',
            keyName: 'curId',
            rules: [{ required: true, message: '请选择项目名称', trigger: 'change' }],
            span: 16,
            isAutoSelectFirst:true,
            isNeedAuth:true,
            checkStrictly: true,
            disabled:true,
            statusSet:[1,2,3,4,5,6],
            isQueryProjecDetail: true
          },
          // {
          //   type: SelectSubCompany,//'a-select',
          //   keyName: 'branchCompanyId',
          //   label: '所属分公司',
          //   disabled: true,
          //   // attrs: {
          //   //     isDefaultSelected: this.type == 'add',
          //   //     autoSelected: this.type == 'add'
          //   // }
          // },
          // {
          //   type: SelectProject,
          //   keyName: 'projectId',
          //   viewName: 'projectName',
          //   label: '所属项目',
          //   disabled: true,
          //   attrs: {
          //     isClear: this.type == 'add',
          //     // isDefaultSelected: this.type == 'add',
          //     autoSelected: this.type == 'add', //this.editable,
          //     isEdit: this.type !== 'add',
          //     keyValue: 'branchCompanyId',
          //     form: this.form,
          //   },
          //   selectChange: (e) => {
          //     if (this.editable) {
          //       queryTechnicalDataManagementList({projectId: this.form.projectId}).then((res) => {
          //         if (res.code == 0 && res.data.length) {
          //           this.$message.warn('该项目已有资料管理计划')
          //         }
          //       })
          //     }
          //   }
          // },
          {
            keyName: 'updateUserName',
            type: 'a-input',
            disabled: true,
            label: '填报人',
          },
          {
            keyName: 'updateTime',
            type: 'a-input',
            disabled: true,
            label: '填报时间',
          },
          {
            keyName: 'technicalDataManagementFile',
            type: UploadList,
            span: 24,
            disabled: (!this.editable),
            label: '附件上传',
            attrs: {
              businessCode: 'FACE05',
              uploadArrys: this.technicalDataManagementFileInfo,
              isShow: this.editable
            },
            fileArrysData: (e) => {
              this.technicalDataManagementFileInfo = [...e]
              this.$nextTick(() => {
                this.$refs.customForm.validateField('technicalDataManagementFile')
              })
            }
          },
        ]
      },
    },
    watch: {
      technicalDataManagementFileInfo(val) {
        if (val) {
          this.form.technicalDataManagementFile = val.map(v => v.commonId)
        }
      }
    },
    methods: {
      selectOrg(val) {
        if(val.length&&val.length < 2) {
          this.form.branchCompanyId = val[val.length - 1].orgId
        } else if(val.length >= 2 ) {
          this.form.branchCompanyId = val[val.length - 2].orgId
          this.form.projectName = val[val.length - 1].projectName
          this.form.projectId = val[val.length - 1].projectId
              if (this.editable) {
                queryTechnicalDataManagementList({projectId: this.form.projectId}).then((res) => {
                  if (res.code == 0 && res.data.length) {
                    if(this.type == 'add'){
                        this.$message.warn('该项目已有资料管理计划')
                    }
                  }
                })
              }
        }
      },
      getDetail() {
        this.loading = true
        getDetails({id: this.$route.query.id}).then((res) => {
          this.loading = false
          if (res.code == 0) {
            const data = res.data
            this.form.id = data.id
            this.form.curId = data.projectId ? data.projectId : this.form.projectId
            this.form.branchCompanyId = data.branchCompanyId
            this.form.projectId = data.projectId
            this.form.branchCompanyName = data.branchCompanyName
            this.form.projectName = data.projectName
            this.form.updateUserName = data.updateUserName
            this.form.updateUserId = data.updateUserId
            this.technicalDataManagementFileInfo = data.technicalDataManagementFileInfo
            this.form.updateTime = data.updateTime
            
          }
        }).catch(() => {
          this.loading = false
        })
      },
      save(back = true) {
        const params = { ...this.form }
        params.auditStatus = '0'
        this.defloading = true
        const api = this.type == 'add' ? addList : editList
        api(params).then((res) => {
          this.defloading = false
          if (res.code == 0) {
            this.$message.success('保存草稿成功')
            if (back) {
              this.$close()
            } else {
              this.getDetail()
            }
          }
        }).catch(() => {
          this.defloading = false
        })
      },
      handleSubmit() {
       this.setProcessConfig = null;
        this.$refs.customForm.validate((valid) => {
          if (valid) {
            const params = { ...this.form }

            params.auditStatus = '1'
            const api = this.type == 'add' ? addList : editList
            const msg = this.type == 'add' ? '添加' : '编辑'
            this.subloading = true
            this.submitApprovalConfirm(this.processConfig, (flag)=>{
              if(!flag) {
                this.subloading = false
                return
              }
            api(params).then((res) => {
              this.subloading = false
              if (res.code == 0) {
                this.$message.success(msg+'成功')
                 //提交时，返回id
                this.businessId = res.data.id;
                this.setProcessConfig = this.processConfig;
              }
            }).catch(() => {
              this.subloading = false
            })

            })
          } else {
            this.$message.warning("请填写完所有必填项信息")
          }
        })
      },
    },
}
</script>

<style lang="less" scoped>
</style>
